﻿using UnityEngine;

/// <summary>
/// 攀爬
/// </summary>
public class FSMStateClimb : FSMStateBase
{
    protected override string getName => "climb";

    private float speed { get; } = UnitDefine.BASE_CLIMB_SPEED;
    
    private Vector3 m_SavedDiretion;

    protected override void OnEnter()
    {
        fsm.entity.physics.groundChecker.colliBox.isTrigger = true;
        fsm.entity.physics.useGravity = false;
        fsm.entity.physics.velocity = Vector2.zero;
        m_SavedDiretion = fsm.entity.animator.direction;
        fsm.entity.animator.direction = Vector3.one;
    }

    protected override void OnExit()
    {
        fsm.entity.physics.groundChecker.colliBox.isTrigger = false;
        fsm.entity.physics.useGravity = true;
        fsm.entity.animator.direction = m_SavedDiretion;
    }

    protected override void OnExecute()
    {
        var axis = fsm.parameters.GetParam<float>("verticalAxis");
        Climb(axis);
    }

    private void Climb(float axis)
    {
        if (!fsm.entity.physics.climberChecker.isClimber) return;
        var pos = new Vector2(fsm.entity.physics.climberChecker.currentClimberInfo.center.x, fsm.entity.transform.position.y);
        pos += Vector2.up * speed * Time.deltaTime * axis;
        fsm.entity.transform.position = pos;
    }
}